package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.ShoppingMapper;
import com.jt.pojo.ItemList;
import com.jt.pojo.Shopping;
import com.jt.vo.NumAndPriceVO;
import com.jt.vo.ShoppingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ShoppingServicelmpl implements ShoppingService{
    @Autowired
    private ShoppingMapper shoppingMapper;

    @Override
    public void ShoppingAdd(ShoppingVo shoppingVo) {
        Shopping shopping =shoppingMapper.selectOne(new QueryWrapper<Shopping>()
                .eq("name", shoppingVo.getItemList().getName())
                .eq("parent_id", shoppingVo.getParentId())
        );
        if (shopping==null){
            Shopping newShopping=shoppingVo.getItemList();
            newShopping.setParentId(shoppingVo.getParentId());
            newShopping.setNummer(shoppingVo.getNum());
            shoppingMapper.insert(newShopping);
        }else {
            Integer num=shoppingVo.getNum();
            Integer id=shopping.getId();
            shoppingMapper.updateShopping(num,id);
        }
    }

    @Override
    public NumAndPriceVO selectNumAndPrice(Integer id) {
        List<Shopping> list=shoppingMapper.selectList(new QueryWrapper<Shopping>()
                .eq("parent_id", id)
        );
        if(list.size()<1){
            return null;
        }

        int price=0;
        for (Shopping shopping : list){
            price+=shopping.getNummer()*shopping.getPrice();
        }
        int num=shoppingMapper.iNum(id);
        return new NumAndPriceVO(num,price);
    }
}
